home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / xdme_1.84_src.lha / XDME / Util / Var / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-22  |  4.1 KB  |  217 lines

  1.  
  2. #include <assert.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7. #include "node.h"
  8.  
  9. #include <proto/dos.h>
  10.  
  11. #define Prototype extern
  12. #define STREAM void *
  13. //#define UBYTE  char
  14. //#define STRPTR char *
  15. #define DSTR struct _DSTR *
  16. //#define BOOL int
  17. #include "protos.h"
  18. #include "Dyn.h"
  19.  
  20.  
  21. /* ************************************************** */
  22.  
  23.  
  24. char *expand (const char *tplt, void *node) {
  25.     DSTR buffer = EmptyDyn;
  26.     char * b2;
  27.     int loops = 1; /* they are already tracing down in NODE_sub */
  28.  
  29.     b2 = strdup(tplt);
  30.     assert (b2 != NULL);
  31.     do {
  32.     ResetNumQueries ();
  33.     if (!flexprintf (&buffer, (void *)DynCat, b2, node, NODE_sub)) {
  34. //puts (DynValue(&buffer));
  35.         assert(("FLEXPRINTF - INTERNAL ERROR", 0));
  36.     } /* if */
  37.     free (b2);
  38.     b2 = strdup(DynValue(&buffer));
  39.     assert(b2 != NULL);
  40.     DynClear(&buffer);
  41.     } while (NumQueries() && --loops);
  42.  
  43.     return b2;
  44. }
  45.  
  46.  
  47. /* ************************************************** */
  48.  
  49.  
  50. void output (char *fname, const char *tpltname) {
  51.     FILE *fi;
  52.     char *bf;
  53.     static DSTR templt = EmptyDyn;
  54.  
  55.     DynClear(&templt);
  56.     if (!DynGetFile(&templt, tpltname)) {
  57.     fprintf (stderr, "can't get templatefile `%s'!\n", tpltname);
  58.     return;
  59.     } /* if */
  60.  
  61. //fprintf (stderr, "sorting\n");
  62. //    LIST_sort(TopNodes, FMODE_NAME);
  63. fprintf (stderr, "sending\n");
  64.  
  65.     if ((bf = expand ( DynValue(&templt), NULL))) {
  66.  
  67.     if (fname)
  68.         if ((fi = fopen (fname, "w"))) {
  69.         fputs(bf, fi);
  70.         fclose (fi);
  71.         } else {
  72.         fprintf (stderr, "FILE IO error\n");
  73.         exit (-1);
  74.         } /* if */
  75.     free(bf);
  76.     } else {
  77.     fprintf (stderr, "error in template expansion!\n");
  78.     exit (-1);
  79.     } /* if */
  80.  
  81. } /* output */
  82.  
  83.  
  84. /* ************************************************** */
  85.  
  86. int actfile    = 0;
  87. int numfiles    = 0;
  88. int numtmplts    = 0;
  89. char *files[100];
  90. char *filename    = "STDIN";
  91. //char *outfile   = NULL;
  92. char *tmplts[100];
  93. char *inittext    = "";
  94. char outname[256];
  95.  
  96. extern FILE *yyin;
  97. extern int lineno;
  98.  
  99. FILE * nextfile (void) {
  100.     FILE *fi;
  101.     lineno = 0;
  102.     while (numfiles > actfile) {
  103.     filename = files[actfile++];
  104.     printf ("Trying to open inputfile `%s'\n", filename);
  105.     if ((fi = fopen (filename, "r"))) {
  106.         fclose (yyin);
  107.         yyin = fi;
  108.         return fi;
  109.     } /* if */
  110.     fprintf (stderr, "Parser: can't open inputfile `%s'!\n", filename);
  111.     } /* while */
  112.     printf ("no [more] inputfiles\n");
  113.     return NULL;
  114. } /* nextfile */
  115.  
  116.  
  117.  
  118. int usage (void) {
  119.     fprintf (stderr, "GenParser [-d] [[-o outfile] -t tmpltfile] -i <ifdef-symbol> infiles ...\n");
  120.     return 10;
  121. } /* usage */
  122.  
  123.  
  124.  
  125. int main (int ac, char **av) {
  126.     extern int yy_flex_debug;
  127.     extern int yydebug;
  128.     extern FILE *yyin;
  129.     int argno = 0;
  130.     int dump  = 0;
  131.     int i;
  132.  
  133.     yy_flex_debug = 0;
  134.     yydebug      = 0;
  135.  
  136.     while (++argno < ac) {
  137.     if (strcmp(av[argno], "-d") == 0) {
  138.         yydebug      = 1;
  139.         yy_flex_debug = 1;
  140.         continue;
  141.     } /* if */
  142.     if (strcmp(av[argno], "-t") == 0) {
  143.         if (++argno < ac)
  144.         tmplts[numtmplts++] = av[argno];
  145.         else
  146.         return usage();
  147.         continue;
  148.     } /* if */
  149.     //if (strcmp(av[argno], "-o") == 0) {
  150.     //    if (++argno < ac)
  151.     //      outfile = av[argno];
  152.     //    else
  153.     //      return usage();
  154.     //    continue;
  155.     //} /* if */
  156.     if (strcmp(av[argno], "-i") == 0) {
  157.         if (++argno < ac)
  158.         inittext = av[argno];
  159.         else
  160.         return usage();
  161.         continue;
  162.     } /* if */
  163.     if (strcmp(av[argno], "-dump") == 0) {
  164.         dump = 1;
  165.         continue;
  166.     } /* if */
  167.  
  168.     files[numfiles++] = av[argno];
  169.     } /* while */
  170.  
  171.     if (!inittext)
  172.     return usage();
  173.  
  174.     NODE_init();
  175.  
  176.     if (numfiles)
  177.     if (!nextfile())
  178.         return 20;
  179.  
  180.     {
  181.     extern int yyparse(void);
  182.     extern int errors;
  183.  
  184.     if (yyparse() || errors)
  185.         return 10;
  186.     }
  187.  
  188.     if (dump) {
  189.     extern void NODE_TREE(void*, void*);
  190.     NODE_TREE(NULL,NULL);
  191.     }
  192.  
  193.     if (numtmplts)
  194.     for (i = 0; i < numtmplts; ++i) {
  195.         int len;
  196.  
  197.         strcpy (outname, tmplts[i]);
  198.  
  199.         len = strlen (outname);
  200.  
  201.         /* ---- if there is a ".tplt", remove it, else prepend "gen_" */
  202.         if (!stricmp (outname + len - 5, ".tplt"))
  203.         outname [len -5] = 0;
  204.         else
  205.         strins (FilePart(outname), "gen_");
  206.  
  207.         output (outname, tmplts[i]);
  208.     } /* for */
  209.  
  210.     else return 5;
  211.  
  212.     return 0;
  213. }
  214.  
  215.  
  216.  
  217.